package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.CouponServiceImpl;
import com.cskaoyan.util.*;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月30日 9:45
 */
@WebServlet("/admin/coupon/*")
public class AdminCouponServlet extends WdBaseServlet {
    CouponService couponService = new CouponServiceImpl();

    @SneakyThrows
    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入优惠价查询功能");
        String name = request.getParameter("name");
        String typeStr = request.getParameter("type");
        Short type = StringUtil.isNotEmpty(typeStr) ? Short.parseShort(typeStr) : null;
        Short status = StringUtil.isNotEmpty(request.getParameter("status")) ? Short.parseShort(request.getParameter("status")) : null;
        Integer page = StringUtil.isNotEmpty(request.getParameter("page")) ? Integer.parseInt(request.getParameter("page")) : null;
        Integer limit = StringUtil.isNotEmpty(request.getParameter("limit")) ? Integer.parseInt(request.getParameter("limit")) : null;
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        CommonData<MarketCoupon> data = couponService.querySelective(name, type, status, page, limit, sort, order);
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    private BaseRespVo listuser(HttpServletRequest request, HttpServletResponse response) {
        Integer page = StringUtil.isNotEmpty(request.getParameter("page")) ? Integer.parseInt(request.getParameter("page")) : null;
        Integer limit = StringUtil.isNotEmpty(request.getParameter("limit")) ? Integer.parseInt(request.getParameter("limit")) : null;
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        Integer couponId = StringUtil.isNotEmpty(request.getParameter("couponId")) ? Integer.parseInt(request.getParameter("couponId")) : null;

        Integer userId = StringUtil.isNotEmpty(request.getParameter("userId")) ? Integer.parseInt(request.getParameter("userId")) : null;
        Integer status = StringUtil.isNotEmpty(request.getParameter("status")) ? Integer.parseInt(request.getParameter("status")) : null;
        List<MarketCouponUser> marketCouponUsers = couponService.selectUserByCouponId(couponId);

        if (userId != null) {
            List<MarketCouponUser> list1 = couponService.selectUserByUserId(userId, couponId);
            CommonData data = CommonData.data(list1);
            return BaseRespVo.ok(data);
        }

        if (status != null) {
            List<MarketCouponUser> list2 = couponService.selectUserByStatus(status, couponId);
            CommonData data = CommonData.data(list2);
            return BaseRespVo.ok(data);
        }

        CommonData data = CommonData.data(marketCouponUsers);
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(jsonStr, MarketCoupon.class);

        System.out.println(marketCoupon);
        System.out.println("测试");
        couponService.update(marketCoupon);//改了的，就是要返回的
        return BaseRespVo.ok(marketCoupon);
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(jsonStr, MarketCoupon.class);
        couponService.deleteByPhysical(marketCoupon.getId());
        return BaseRespVo.ok();
    }


    @SneakyThrows
    private BaseRespVo read(HttpServletRequest request, HttpServletResponse response) {
        Integer id = StringUtil.isNotEmpty(request.getParameter("id")) ? Integer.parseInt(request.getParameter("id")) : null;
        MarketCoupon marketCoupon = couponService.selectById(id);
        return BaseRespVo.ok(marketCoupon);

    }


    @SneakyThrows
    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketCoupon coupon = JacksonUtil.read(jsonStr, MarketCoupon.class);
        System.out.println("进入添加优惠卷功能");
        BaseRespVo error = CouponUtil.validate(coupon);
        if (error == null) {
            return error;
        }
        // 如果是兑换码类型，则这里需要生成一个兑换码
        if (coupon.getType() == 2) {
            String code = CouponUtil.generateCode();
            coupon.setCode(code);
        }
        coupon.setId(null);
        System.out.println("----------------------添加之前--------------------------");
        System.out.println(coupon);
        //操作时间
        java.util.Date updateTime = new Date();//获得当前时间
        coupon.setAddTime(updateTime);
        coupon.setUpdateTime(updateTime);

        //
        couponService.add(coupon);
        System.out.println("---------------------添加之后-----------------------------");
        System.out.println(coupon);
        return BaseRespVo.ok(coupon);
    }
}
